{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "reliable-frank",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a string\n",
      "732\n",
      "c\n",
      "true\n",
      "<function0>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\u001b[36mlist\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mAny\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m\"a string\"\u001b[39m, \u001b[32m732\u001b[39m, \u001b[32m'c'\u001b[39m, true, <function0>)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val list:List[Any] = List(\n",
    "    \"a string\",\n",
    "    732,\n",
    "    'c',\n",
    "    true,\n",
    "    ()=>\"aaaaa\"\n",
    "\n",
    ")\n",
    "\n",
    "list.foreach(e => println(e))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "specialized-mechanism",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mx\u001b[39m: \u001b[32mLong\u001b[39m = \u001b[32m987654321L\u001b[39m\r\n",
       "\u001b[36my\u001b[39m: \u001b[32mFloat\u001b[39m = \u001b[32m9.8765434E8F\u001b[39m\r\n",
       "\u001b[36mface\u001b[39m: \u001b[32mChar\u001b[39m = \u001b[32m'\\u263a'\u001b[39m\r\n",
       "\u001b[36mnumber\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m9786\u001b[39m"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val x: Long = 987654321\n",
    "val y: Float = x  // 9.8765434E8 (note that some precision is lost in this case)\n",
    "\n",
    "val face: Char = '☺'\n",
    "val number: Int = face  // 9786"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "after-registrar",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "cmd2.sc:3: type mismatch;\n",
      " found   : Float\n",
      " required: Long\n",
      "val z: Long = y  // Does not conform\n",
      "              ^Compilation Failed"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "Compilation Failed"
     ]
    }
   ],
   "source": [
    "val x: Long = 987654321\n",
    "val y: Float = x  // 9.8765434E8\n",
    "val z: Long = y  // Does not conform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ceramic-attention",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mUser\u001b[39m\r\n",
       "\u001b[36muser1\u001b[39m: \u001b[32mUser\u001b[39m = ammonite.$sess.cmd2$Helper$User@32a8d24f"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class User\n",
    "\n",
    "val user1 = new User"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "strange-check",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mPoint\u001b[39m"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Point(var x: Int, var y: Int) {\n",
    "\n",
    "  def move(dx: Int, dy: Int): Unit = {\n",
    "    x = x + dx\n",
    "    y = y + dy\n",
    "  }\n",
    "\n",
    "  override def toString: String =\n",
    "    s\"($x, $y)\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "introductory-monster",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mpoint1\u001b[39m: \u001b[32mPoint\u001b[39m = (2, 3)\r\n",
       "\u001b[36mres4_1\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m2\u001b[39m"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val point1 = new Point(2,3)\n",
    "point1.x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "lasting-frederick",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 3)\n"
     ]
    }
   ],
   "source": [
    "println(point1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "greek-scheme",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mPoint\u001b[39m"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Point (var x : Int = 0,var y:Int = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "oriented-operation",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36maaa\u001b[39m: \u001b[32mPoint\u001b[39m = ammonite.$sess.cmd6$Helper$Point@303b4942\r\n",
       "\u001b[36mpoint1\u001b[39m: \u001b[32mPoint\u001b[39m = ammonite.$sess.cmd6$Helper$Point@1f5d833b\r\n",
       "\u001b[36mpoint2\u001b[39m: \u001b[32mPoint\u001b[39m = ammonite.$sess.cmd6$Helper$Point@696c0077"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val aaa = new Point\n",
    "val point1 = new Point(1)\n",
    "val point2 = new Point(y=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "unlikely-currency",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "println(point1.x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "funky-bhutan",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "println(point2.y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "aerial-punch",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:Out of bounds\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mPoint\u001b[39m\r\n",
       "\u001b[36mpoint1\u001b[39m: \u001b[32mPoint\u001b[39m = ammonite.$sess.cmd15$Helper$Point@7314e9fa"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Point {\n",
    "    private var _x = 0\n",
    "    private var _y = 0\n",
    "    private val bound = 100\n",
    "    \n",
    "    def xi = _x   \n",
    "    def xi_= (newValue :Int): Unit = {\n",
    "        if (newValue < bound) _x = newValue else printWarning\n",
    "     }\n",
    "    \n",
    "    def yi = _y\n",
    "    def yi_= (newValue :Int): Unit = {\n",
    "        if (newValue < bound) _y = newValue else printWarning\n",
    "     }\n",
    "    \n",
    "    private def printWarning = println(\"WARNING:Out of bounds\")\n",
    "}\n",
    "\n",
    "val point1 = new Point\n",
    "point1.xi = 99\n",
    "point1.yi = 111"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "manual-florence",
   "metadata": {},
   "outputs": [],
   "source": [
    "// 我们在用Java的时候经常把一些字段定义为private类型来完成封装，这样外界就无法访问。\n",
    "\n",
    "// 如果外界访问或者修改该字段的时候，只能通过该字段提供的getter和setter方法来实现。\n",
    "\n",
    "// 在Scala中是没有getter和setter一说的。\n",
    "\n",
    "// 用了value 和 value_= 来分别代替了getter和setter。\n",
    "\n",
    "//这里的value可以改 ，但是必须同步更改，两者要相同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "agreed-morocco",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
